package com.jckj.core.service.impl;

import com.jckj.core.annotation.CurrentDataSource;
import com.jckj.core.dao.SysDictMapper;
import com.jckj.core.db.DataSourceEnum;
import com.jckj.core.entity.AgriProduct;
import com.jckj.core.entity.AgriProductExample;
import com.jckj.core.entity.SysDict;
import com.jckj.core.entity.SysDictExample;
import com.jckj.core.enums.SysDictType;
import com.jckj.core.exception.ServiceException;
import com.jckj.core.service.SysDictService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 *     字典维护服务类
 * </p>
 * @author :liukuan
 * @date   : 2018-1-29
 */
@Service
@Transactional(readOnly = true,rollbackFor = ServiceException.class)
public class SysDictServiceImpl implements SysDictService {

    private static Logger LOGGER = LoggerFactory.getLogger(SysDictServiceImpl.class);

    @Autowired
    private SysDictMapper sysDictMapper;
    /**
     * 添加字典维护
     *
     * @param sysDict
     * @return
     */
    @Override
    @Transactional(readOnly = false,rollbackFor = ServiceException.class)
    public int insert(SysDict sysDict) {
        int num=0;
        try {
           num=sysDictMapper.insert(sysDict);
        } catch (Exception e) {
            LOGGER.error("保存字典信息：%s",e);
            throw new ServiceException("保存字典出错",e);
        }
        return num;
    }

    /**
     * 修改字黄维护
     *
     * @param sysDict
     * @return
     */
    @Override
    @Transactional(readOnly = false,rollbackFor = ServiceException.class)
    public int update(SysDict sysDict) {
        int num=0;
        try {
            num=sysDictMapper.updateByPrimaryKeySelective(sysDict);
        } catch (Exception e) {
            LOGGER.error("修改字典信息:%s",e);
            throw new ServiceException("修改字典信息出错",e);
        }
        return num;
    }

    /**
     * 根据主键删除
     *
     * @param id
     * @return
     */
    @Override
    @Transactional(readOnly = false,rollbackFor = ServiceException.class)
    public int deleteById(String id) {
        int num=0;
        try {
            num=sysDictMapper.deleteByPrimaryKey(id);
        } catch (Exception e) {
            LOGGER.error("删除字典信息:%s",e);
            throw new ServiceException("删除字典信息出错",e);
        }
        return num;
    }

    /**
     * 根据主键查询
     * @param id
     * @return
     */
    @Override
    @CurrentDataSource(DataSourceEnum.SLAVE)
    public SysDict findById(String id) {
        return sysDictMapper.selectByPrimaryKey(id);
    }


    @Override
    @CurrentDataSource(DataSourceEnum.SLAVE)
    public List<SysDict> getSysDictListBy(String typeCode) {
        List<SysDict> sysDistList = new ArrayList();
        SysDictExample example = new SysDictExample();
        example.setOrderByClause("sort");
        SysDictExample.Criteria criteria = example.createCriteria();
        criteria.andTypeEqualTo(SysDictType.PRODUCT_TYPE.getTypeCode());
        sysDistList.addAll(sysDictMapper.selectByExample(example));
        return sysDistList;
    }
}
